<template>
  <div class="login-container" @keyup.enter="handleLogin">
    <el-form class="login-form" autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left">
      <div class="logo" style="text-align:center;width:200px;margin:10px auto;">
        <img style="width:100%" src="../../assets/images/logo.png" alt="">
      </div>
      <h3 class="title">供应商库存管理系统</h3>
      <el-form-item prop="username">
        <span class="svg-container svg-container_login">
          <svg-icon icon-class="user" />
        </span>
        <el-input name="username" type="text" v-model="loginForm.username" autoComplete="on" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"></svg-icon>
        </span>
        <el-input name="password" :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on"
          placeholder="请输入密码"></el-input>
        <span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye" /></span>
      </el-form-item>
      <el-form-item class="code" prop="code">
        <span class="svg-container svg-container_login">
          <svg-icon icon-class="user" />
        </span>
        <el-input name="code" type="text" v-model="loginForm.code" autoComplete="off" placeholder="请输入验证码" />
        <div @click="changeImgCode" class="code-img"><img v-if="img_code" :src="img_code"></div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width:100%;" :loading="loading" @click.native.prevent="handleLogin">
          登 &nbsp; &nbsp; 录
        </el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
  import {
    getCodeImg
  } from '@/api/user'
  // import {
  //   isvalidUsername
  // } from '@/utils/validate'
  import {
    encryptString
  } from '@/utils'

  export default {
    name: 'login',
    data() {
      return {
        loginForm: {
          username: '',
          password: '',
          code: '',
          token: ''
        },
        loginRules: {
          username: [{
            required: true,
            message: '请输入管理员账号',
            trigger: 'blur'
          }],
          password: [{
            required: true,
            message: '请输入管理员账号',
            trigger: 'blur'
          }, {
            min: 6,
            message: '密码不能小于6位',
            trigger: 'blur'
          }],
          code: [{
            required: true,
            message: '请输入验证码',
            trigger: 'blur'
          }]
        },
        loading: false,
        pwdType: 'password',
        img_code: '',
        codeParams: {
          tt: 0,
          token: Math.random().toString(36).substr(2),
          type: 1
        }
      }
    },
    methods: {
      showPwd() {
        if (this.pwdType === 'password') {
          this.pwdType = ''
        } else {
          this.pwdType = 'password'
        }
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            const formData = {
              uName: this.loginForm.username.trim(),
              uPwd: encryptString(this.loginForm.password),
              code: this.loginForm.code,
              token: this.codeParams.token
            }
            this.$store.dispatch('Login', formData).then(response => {
              this.loading = false
              this.$router.push({
                path: '/'
              })
            }).catch(() => {
              this.loading = false
              // 重新获取图片验证码
              this.changeImgCode()
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      init() {
        // 获取图片验证码
        this.changeImgCode()
      },
      changeImgCode() {
        this.codeParams.tt = Date.parse(new Date())
        // 获取图片验证码接口
        getCodeImg(this.codeParams)
          .then(response => {
            return 'data:image/png;base64,' + btoa(
              new Uint8Array(response).reduce((data, byte) => data + String.fromCharCode(byte), '')
            )
          }).then(data => {
            this.img_code = data
          })
      }
    },
    mounted() {
      this.init()
    }
  }

</script>

<style rel="stylesheet/scss" lang="scss">
  @import "src/styles/mixin.scss";
  $bg:#2d3a4b;
  $light_gray:#eee;
  /* reset element-ui css */

  .login-container {
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        &:-webkit-autofill {
          -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: #fff !important;
        }
      }
    }
    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
    .code-img {
      position: absolute;
      right: 0;
      width: 150px;
      height: 52px;
      top: 0;
      img {
        height: 100%;
        width: 100%;
      }
    }
  }

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;
  .login-container {
    position: fixed;
    height: 100%;
    width: 100%;
    background-color: $bg;
    .login-form {
      position: absolute;
      left: 0;
      right: 0;
      width: 520px;
      padding: 35px 35px 15px 35px;
      margin: 120px auto;
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }
    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .title {
      font-size: 26px;
      font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }

</style>
